package org.sgroup.imenu.service;

import javax.xml.parsers.*;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.*;
import org.xml.sax.InputSource;

import android.content.Context;
import android.util.Log;

import java.io.*;

public class XmlDocument {

	private static DocumentBuilderFactory factory = DocumentBuilderFactory
			.newInstance();

	public static Document Load(String path) {
		Document doc = null;
		try {
			DocumentBuilder builder = factory.newDocumentBuilder();
			File f = new File(path);
			doc = builder.parse(f);
		} catch (Exception e) {
		}
		return doc;
	}

	public static Document LoadXml(String content) {
		Document doc = null;
		try {
			DocumentBuilder builder = factory.newDocumentBuilder();
			doc = builder.parse(new ByteArrayInputStream(content.getBytes()));
			// StringReader rd=new StringReader(content);
			// doc=builder.parse(new InputSource(rd));
		} catch (Exception e) {
			Log.i("LoadXML", e.toString());
		}
		return doc;
	}

	public static Document LoadStream(InputStream source) {
		Document doc = null;
		try {
			DocumentBuilder builder = factory.newDocumentBuilder();
			doc = builder.parse(source);
		} catch (Exception e) {
		}
		return doc;
	}

	public static Node selectSingleNode(Document doc, String xpath) {
		Node node = null;
		try {
			XPath xp = XPathFactory.newInstance().newXPath();
			node = (Node) xp.evaluate(xpath, doc, XPathConstants.NODE);
		} catch (Exception e) {
		}
		return node;
	}

	public static NodeList selectNodes(Document doc, String xpath) {
		NodeList list = null;
		try {
			XPath xp = XPathFactory.newInstance().newXPath();
			list = (NodeList) xp.evaluate(xpath, doc, XPathConstants.NODESET);
		} catch (Exception e) {
		}
		return list;
	}

	public static NodeList selectNodes(Element element, String xpath) {
		NodeList list = null;
		try {
			XPath xp = XPathFactory.newInstance().newXPath();
			list = (NodeList) xp.evaluate(xpath, element,
					XPathConstants.NODESET);
		} catch (Exception e) {
		}
		return list;
	}

	public static Boolean Save(Document doc, String path, String filename) {
		Boolean result = true;
		try {
			File f = new File(path, filename);
			TransformerFactory transfac = TransformerFactory.newInstance();
			Transformer trans = transfac.newTransformer();
			DOMSource src = new DOMSource(doc);
			StreamResult stream = new StreamResult(f);
			trans.transform(src, stream);
		} catch (Exception e) {
			result = false;
		}
		return result;
	}

	public static Boolean Save(String content, String path, String filename) {
		Boolean result = true;
		try {
			File f = new File(path, filename);
			FileOutputStream fOut = new FileOutputStream(f, false);
			OutputStreamWriter osw = new OutputStreamWriter(fOut);
			osw.write(content);
			osw.flush();
			osw.close();
		} catch (Exception e) {
			result = false;
		}
		return result;
	}

	public static Boolean SaveAsset(String content, String filename, Context ctx) {
		Boolean result = true;
		try {
			FileOutputStream fos = ctx.openFileOutput(filename,
					Context.MODE_PRIVATE);
			fos.write(content.getBytes());

			fos.close();
		} catch (Exception e) {
			result = false;
		}
		return result;
	}

	public static Document createDocument() {
		Document doc = null;
		try {
			DocumentBuilder builder = factory.newDocumentBuilder();
			doc = builder.newDocument();
		} catch (Exception e) {
		}
		return doc;
	}

	public static Element createElement(Document doc, String tagName) {
		Element element = doc.createElement(tagName);
		return element;
	}

}
